<template>
  <div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{item.name}}</dt>
      <dd>
        <template v-for="val in item.values" :key="val.name">
        <img  :src="val.picture" :title="val.name" v-if="val.picture" @click="change(item,val)" :class="{selected:val.selected,disabled:val.disabled}" >
        <span v-else @click='change(item,val)' :class="{selected:val.selected,disabled:val.disabled}">{{val.name}}</span>
        </template>
      </dd>
    </dl>
  </div>
</template>
<script>
// sku路径
import powerset from '@/vender/power-set'
// import { reactive } from 'vue'
// 默认选中
const initDefaultSelected = (goods, skuId) => {
  const sku = goods.skus.find(item => item.id === skuId)
  goods.specs.forEach((val, i) => {
    const vals = val.values.find(item => item.name === sku.specs[i].valueName)
    vals.selected = true
  })
}
export default {
  name: 'GoodsSku',
  props: {
    goods: {
      type: Object,
      default: () => ({})
    },
    // 接收传来的skuid
    skuId: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    console.log(props.skuId)
    const spliter = '*'
    const pathDic = {}
    // 获取选中数组
    const skuChoice = (specs) => {
      const arr = []
      specs.forEach(item => {
        const selecteVal = item.values.find(val => val.selected)
        arr.push(selecteVal ? selecteVal.name : undefined)
      })
      return arr
    }
    // 初始化禁用
    const specificationButtonDisable = (spec, pathDic) => {
      spec.forEach((item, i) => {
        const selectedValues = skuChoice(spec)
        console.log(selectedValues)
        item.values.forEach(val => {
          if (val.selected) return
          selectedValues[i] = val.name
          const key = selectedValues.filter(value => value).join(spliter)
          val.disabled = !pathDic[key]
        })
      })
    }
    const change = (item, val) => {
      console.log(12345)
      if (val.disabled) return
      if (val.selected) {
        val.selected = false
      } else {
        item.values.forEach(vals => {
          vals.selected = false
        })
        val.selected = true
      }
      specificationButtonDisable(props.goods.specs, pathDic)
      // 向父组件传sku值选中的值(判断是否其选中所有规格)
      fatherPassSku(props)
    }
    // 得到goods中skus数据
    const skusdata = (sku) => {
      sku.forEach((item) => {
        if (item.inventory > 0) {
          const valueArry = item.specs.map(val => val.valueName)
          const valuePowerSet = powerset(valueArry)
          valuePowerSet.forEach(arr => {
            const key = arr.join(spliter)
            if (pathDic[key]) {
              pathDic[key].push(item.id)
            } else {
              pathDic[key] = [item.id]
            }
          })
        }
      })
    }

    const pathMap = skusdata(props.goods.skus)

    // 根据skuId初始化选中
    if (props.skuId) initDefaultSelected(props.goods, props.skuId)
    // 初始化时判断
    specificationButtonDisable(props.goods.specs, pathDic)
    console.log(pathDic)
    // 向父组件传sku值选中的值(判断是否其选中所有规格)
    const fatherPassSku = (props) => {
      const passSku = skuChoice(props.goods.specs).filter(v => v)
      if (passSku.length === props.goods.specs.length) {
        console.log(passSku.join(spliter))
        // 拿到选中规格id
        const passSkuId = pathDic[passSku.join(spliter)]
        const sku = props.goods.skus.find(i => i.id === passSkuId[0])
        // 向父组件传递过去
        emit('change', {
          skuId: sku.id,
          price: sku.price,
          oldPrice: sku.oldPrice,
          inventory: sku.inventory,
          specsText: sku.specs.reduce((q, a) => `${q} ${a.name}: ${a.valueName}`, '').trim()
        })
      } else {
        // 如果规格没有选完成返回空
        emit('change', {})
      }
    }
    fatherPassSku(props)
    return { change, pathMap, pathDic }
  }
}
</script>
<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
  &.selected {
    border-color: @xtxColor;
  }
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
dl{
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt{
        width: 50px;
        color:#999;
    }
    dd{
        display: 1;
        color:#666;
    >img{
        width: 50px;
        height: 50px;
        .sku-state-mixin ()
    }
    >span{
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding:0 20px;
        .sku-state-mixin ()
    }
    }
}}
</style>
